class Giving {
    /**
     * 邀请赠送
     * @api {GET}         /              邀请赠送
     * @apiName           /giving
     * @apiGroup          Giving
     * @apiVersion 0.1.0
     * @apiParam          {Int}            page        页数
     * @apiParam          {Int}            limit       条数
     * @apiParam          {Int}            type        类型
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": {
            "count": 1,
            "rows": []
        }
      }
     */
    async index(ctx) {
        const {
            db,
            request: {
                query: { page = 1, limit = 10, type }
            }
        } = ctx;

        let condition = {
            limit: parseInt(limit),
            offset: (parseInt(page) - 1) * 10,
            where: {
                type: 1
            }
        };

        if (parseInt(type) !== 1) {
            condition.where.type = -1;
        }

        let data = await db.Giving.findAndCountAll(condition);
        ctx.success(data);
    }
    /**
     * 新增
     * @api {POST}         /              新增
     * @apiName           /giving
     * @apiGroup          Giving
     * @apiVersion 0.1.0
     * @apiParam          {Int}            num        数量
     * @apiParam          {Int}            type       类型
     * @apiParam          {String}         desc       描述
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": []
       }
     */
    async create(ctx) {
        const {
            db,
            request: {
                body: { num, type, desc, memberId }
            }
        } = ctx;

        let condition;
        console.log(type);
        if (type === 1) {
            condition = {
                where: {
                    memberId: memberId,
                    type: 1
                },
                defaults: {
                    day: num,
                    desc: desc,
                    memberId: memberId,
                    type: 1
                }
            };
            console.log(condition);
            db.Giving.findCreateFind(condition);
        } else {
            let data = await db.Giving.findOne({ where: { id: 1 } });
            data.limit = num;
            data.desc = desc;
            data.save();
        }
        ctx.success();
    }
    /**
     * 删除
     * @api {DELETE}       /:id              删除
     * @apiName           /giving
     * @apiGroup          Giving
     * @apiVersion 0.1.0
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": []
       }
     */
    async deletes(ctx) {
        const {
            db,
            params: { id }
        } = ctx;
        db.Giving.destroy({ where: { id: id } });
        ctx.success();
    }
    /**
     * 查询会员卡
     * @api {DELETE}      /member              查询会员卡
     * @apiName           /member
     * @apiGroup          Giving
     * @apiVersion 0.1.0
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": []
       }
     */
    async member(ctx) {
        const { db, Op } = ctx;
        let data = await db.Members.findAll({
            where: {
                id: {
                    [Op.ne]: 0
                }
            }
        });
        ctx.success(data);
    }
}
module.exports = new Giving();
